package nz.ac.massey.doi_information_extractor.gui;

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import nz.ac.massey.doi_information_extractor.data.Author;

@SuppressWarnings("serial")
public class CreateAuthorDialog extends JDialog implements ActionListener {

	private static CreateAuthorDialog dialog;
	private static Author newAuthor;
	private Author inputAuthor;
	private JButton createButton, cancelButton;
	private JTextField firstNameTextField, lastNameTextField, organisationTextField, departmentTextField, typeTextField, userIdField;
	private String dialogType;

	public static Author showDialog(Component frameComp, Author author, String type) {
		Frame frame = JOptionPane.getFrameForComponent(frameComp);
		dialog = new CreateAuthorDialog(frame, author, type);
		dialog.setVisible(true);
		//dialog = null;

		return newAuthor;
	}

	public CreateAuthorDialog(Frame frame, Author inputAuthor, String type) {
		super(frame, type, true);

		this.inputAuthor = inputAuthor;
		this.dialogType = type;
		init();

		if (type.equals("New Author")) {
			createButton.setText("Create");
		} else if (type.equals("Edit Author")) {
			createButton.setText("Save");
		}
	}

	private void init() {
		JPanel mainPanel = new JPanel();

		firstNameTextField = new JTextField();
		lastNameTextField = new JTextField();
		organisationTextField = new JTextField();
		departmentTextField = new JTextField();
		typeTextField = new JTextField();
		userIdField = new JTextField();

		JLabel jLabel1 = new JLabel("Author First Name");
		JLabel jLabel2 = new JLabel("Author Last Name");
		JLabel jLabel3 = new JLabel("Organisation");
		JLabel jLabel4 = new JLabel("Department");
		JLabel jLabel5 = new JLabel("User Massey Id");
		JLabel jLabel6 = new JLabel("Author Type");

		createButton = new JButton();
		createButton.addActionListener(this);

		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);

		javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(mainPanelLayout);
		mainPanelLayout
				.setHorizontalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								mainPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																mainPanelLayout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																		.addGroup(
																				mainPanelLayout
																						.createSequentialGroup()
																						.addComponent(
																								jLabel1)
																						.addPreferredGap(
																								javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																						.addComponent(
																								firstNameTextField,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								186,
																								Short.MAX_VALUE))
																		.addGroup(
																				mainPanelLayout
																						.createSequentialGroup()
																						.addGroup(
																								mainPanelLayout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(
																												jLabel2)
																										.addComponent(
																												jLabel3)
																										.addComponent(
																												jLabel6)
																										.addComponent(
																												jLabel5))
																						.addPreferredGap(
																								javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																						.addGroup(
																								mainPanelLayout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(
																												userIdField,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												187,
																												Short.MAX_VALUE)
																										.addGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING,
																												mainPanelLayout
																														.createSequentialGroup()
																														.addComponent(
																																createButton)
																														.addPreferredGap(
																																javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																																41,
																																Short.MAX_VALUE)
																														.addComponent(
																																cancelButton))
																										.addComponent(
																												lastNameTextField,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												187,
																												Short.MAX_VALUE)
																										.addComponent(
																												organisationTextField,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												187,
																												Short.MAX_VALUE)
																										.addComponent(
																												departmentTextField,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												187,
																												Short.MAX_VALUE)
																										.addComponent(
																												typeTextField,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												187,
																												Short.MAX_VALUE))))
														.addGroup(
																mainPanelLayout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel4)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				220,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addGap(119, 119, 119)));
		mainPanelLayout
				.setVerticalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								mainPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																firstNameTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																lastNameTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel2))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																organisationTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel3))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																departmentTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																typeTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																userIdField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel5))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																createButton)
														.addComponent(
																cancelButton))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		getContentPane().add(mainPanel);

		pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

		setInputAuthor();
	}

	private void setInputAuthor() {
		if (inputAuthor == null)
			return;

		firstNameTextField.setText(inputAuthor.getAuthorFirstName());
		lastNameTextField.setText(inputAuthor.getAuthorLastName());
		organisationTextField.setText(inputAuthor.getOrganisation());
		departmentTextField.setText(inputAuthor.getDepartment());
		typeTextField.setText(inputAuthor.getAuthorType());
		userIdField.setText(inputAuthor.getUserId());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == createButton) {
			if(firstNameTextField.getText().equals("") && lastNameTextField.getText().equals(""))
				return;

			createAuthor();
		} else if (e.getSource() == cancelButton) {
			newAuthor = null;
		}
		this.setVisible(false);
		this.dispose();
	}

	private void createAuthor() {
		newAuthor = new Author();

		newAuthor.setAuthorFirstName(firstNameTextField.getText());

		newAuthor.setAuthorLastName(lastNameTextField.getText());

		newAuthor.setAuthorType(typeTextField.getText());

		newAuthor.setDepartment(departmentTextField.getText());

		newAuthor.setUserId(userIdField.getText());

		newAuthor.setOrganisation(organisationTextField.getText());

		if (inputAuthor != null && dialogType.equals("Edit Author")){
			newAuthor.setAuthorId(inputAuthor.getAuthorId());
		}
	}
}
